﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugTracker.Models;
using BugTracker.Helper;
using System.Data;
using System.Data.Entity;
using BugTracker.Controllers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
//using System.Data.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;

namespace BugTracker.Controllers
{
    public class AdminController : Controller
    {
         Bug_db toget = new Bug_db();
         SessionHelper sessionhelper = new SessionHelper();

        //
        // GET: /AdminDashboard/
         public ActionResult Index()
         {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    int role_count = (from r in toget.Roles select r).Count();
                    ViewBag.Total = role_count;

                    return View(toget.Roles.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
         }
       
        //Create New User
        public ActionResult CreateUser()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["role_idPK"] = (toget.Roles.OrderBy(r => r.role_idPK)).ToList();
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save new created User
        [HttpPost]
        public ActionResult CreateUser(User users)
        {
            if (ModelState.IsValid)
            {
                toget.Users.Add(users);
                users.datecreated = System.DateTime.Now.ToString();
                toget.SaveChanges();
                return RedirectToAction("DeleteUser", "Admin");
            }

            return View();
        }

        //Edit User
        public ActionResult Edit(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["role_idPK"] = (toget.Roles.OrderBy(r => r.role_idPK)).ToList();
                    User user = toget.Users.Find(id);
                    return View(user);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save edited User
        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                toget.Entry(user).State = EntityState.Modified;
                user.user_idPK = user.user_idPK;
                toget.SaveChanges();
                return RedirectToAction("DeleteUser");
            }
            return View(user);
        }
 
        public ActionResult EditUser()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Users.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //View user details
        public ActionResult Details(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    User user = toget.Users.Find(id);
                    return View(user);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        public ActionResult ViewUsers()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    var displayUsers = (from u in toget.Users
                                        join r in toget.Roles on u.role_idFK equals r.role_idPK
                                        where u.role_idFK == r.role_idPK
                                        select new UserRole
                                        {
                                            user_name = u.user_name,
                                            first_name = u.first_name,
                                            last_name = u.last_name,
                                            role_name = r.role_name,
                                        });

                    return View(displayUsers.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }
  
        public ActionResult DeleteUser()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Users.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Delete User
        public ActionResult Delete(FormCollection fcNotUsed, int id = 0)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    User user = toget.Users.Find(id);
                    if (user == null)
                    {
                        return HttpNotFound();
                    }
                    toget.Users.Remove(user);
                    toget.SaveChanges();
                    return RedirectToAction("DeleteUser");
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Delete User Confirmation
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id = 0)
        {
            User user = toget.Users.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            toget.Users.Remove(user);
            toget.SaveChanges();
            return RedirectToAction("DeleteUser");
        }

        //PROJECT
        //View Project Table
        public ActionResult Project()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Projects.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });

        }

        public ActionResult CreateProject()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["Tasks"] = (toget.Tasks.OrderBy(c => c.task_assignID)).ToList();
                    ViewData["QA"] = toget.Users.Where(q => q.role_idFK == 3).ToList();
                    ViewData["Developer"] = toget.Users.Where(d => d.role_idFK == 2).ToList();
                    ViewData["Status"] = (toget.Status.OrderBy(r => r.status_id)).ToList();

                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save new created Project
        [HttpPost]
        public ActionResult CreateProject(Task task, Project project)
        {

            project.project_datecreated = System.DateTime.Now.ToString();
            if (ModelState.IsValid)
            {

                toget.Projects.Add(project);
                toget.SaveChanges();
                return RedirectToAction("Project");
            }

            return View(project);
        }

        // View Project Details 
        public ActionResult DetailsProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = toget.Projects.Find(id);
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Edit Project
        public ActionResult EditProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = toget.Projects.Find(id);
                    ViewData["Status"] = (toget.Status.OrderBy(r => r.status_id)).ToList();
                    ViewData["Tasks"] = (toget.Tasks.OrderBy(c => c.task_assignID)).ToList();
                    ViewData["QA"] = toget.Users.Where(q => q.role_idFK == 3).ToList();
                    ViewData["Developer"] = toget.Users.Where(d => d.role_idFK == 2).ToList();

                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save edited Project 
        [HttpPost]
        public ActionResult EditProject(Project project)
        {

            if (ModelState.IsValid)
            {

                toget.Entry(project).State = EntityState.Modified;
                toget.SaveChanges();
                return RedirectToAction("Project");
            }
            return View(project);

        }

        //Delete Project
        public ActionResult DeleteProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = toget.Projects.Find(id);
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Delete Project
        [HttpPost, ActionName("DeleteProject")]
        public ActionResult DeleteProjectConfirmed(int id)
        {
            Project project = toget.Projects.Find(id);
            toget.Projects.Remove(project);
            toget.SaveChanges();
            return RedirectToAction("Project");
        }

        protected override void Dispose(bool disposing)
        {
            toget.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult ExportToExcel()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //ISSUE
        public ActionResult Issue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(toget.Issues.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Create New Issue
        public ActionResult CreateIssue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["ProjectName"] = (toget.Projects.OrderBy(p => p.project_idPK)).ToList();
                    //ViewData["ProjectAssign"] = (db.Projects.OrderBy(p => p.user_idFK)).ToList();
                    ViewData["Developer"] = toget.Users.Where(d => d.role_idFK == 2).ToList();
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        
        //Save new created Issue
        [HttpPost]
        public ActionResult CreateIssue(Issue issue)
        {

            if (ModelState.IsValid)
            {

                issue.date_identified = System.DateTime.Now.ToString();
                toget.Issues.Add(issue);
                toget.SaveChanges();
                return RedirectToAction("Issue");
            }

            return View(issue);
        }


        // View Issue Details 
        public ActionResult DetailsIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = toget.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }


        //Edit issue (only status can be edited)
        public ActionResult EditIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    //ViewData["ProjectName"] = (db.Projects.OrderBy(p => p.project_idPK)).ToList();
                    ViewData["ProjectName"] = (toget.Projects.OrderBy(p => p.project_idPK)).ToList();

                    Issue issue = toget.Issues.Find(id);
                    ViewBag.dateidentified = issue.date_identified;
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save edited Issue 
        [HttpPost]
        public ActionResult EditIssue(Issue issue)
        {

            if (ModelState.IsValid)
            {
                toget.Entry(issue).State = EntityState.Modified;
                toget.SaveChanges();
                return RedirectToAction("Issue");
            }
            return View(issue);
        }

        //Delete Issue
        public ActionResult DeleteIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = toget.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Delete Issue
        [HttpPost, ActionName("DeleteIssue")]
        public ActionResult DeleteIssueConfirmed(int id)
        {
            Issue issue = toget.Issues.Find(id);
            toget.Issues.Remove(issue);
            toget.SaveChanges();
            return RedirectToAction("Issue");
        }

        //REPORTS
        public ActionResult ViewReports()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }
    }
}
